package com.yanqu.road.timer;

import com.yanqu.road.task.CheckDeadLockTask;
import com.yanqu.road.task.DirectMemoryMonitorTask;
import com.yanqu.road.server.TempServer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class GeneralAgentTimerMgr {

    private static final Logger log = LogManager.getLogger(GeneralAgentTimerMgr.class.getName());

    private static ScheduledExecutorService timerExecutorService;

    private static ConnectionCheckTask connectionCheckTask = null;

    private static DirectMemoryMonitorTask directMemoryMonitorTask = null;

    private static CrossServerConnCheckTask crossServerConnCheckTask = null;

    private static CheckDeadLockTask checkDeadLockTask = null;

    public static synchronized boolean initGeneralTimers() {
        timerExecutorService = TempServer.getTimerExecutorService();

        int interval = 0;
        interval = 10;
        if (connectionCheckTask == null) {
            connectionCheckTask = new ConnectionCheckTask();
            timerExecutorService.scheduleWithFixedDelay(connectionCheckTask, 10, interval, TimeUnit.SECONDS);
        }
        if (directMemoryMonitorTask == null) {
            directMemoryMonitorTask = new DirectMemoryMonitorTask();
            timerExecutorService.scheduleWithFixedDelay(directMemoryMonitorTask, 60, 60, TimeUnit.SECONDS);
        }
        if (crossServerConnCheckTask == null) {
            crossServerConnCheckTask = new CrossServerConnCheckTask();
            timerExecutorService.scheduleWithFixedDelay(crossServerConnCheckTask, 60, 120, TimeUnit.SECONDS);
        }

        if (checkDeadLockTask == null) {
            checkDeadLockTask = new CheckDeadLockTask();
            timerExecutorService.scheduleWithFixedDelay(checkDeadLockTask, 0, 30, TimeUnit.SECONDS);
        }
        return true;
    }
}
